<?php
namespace Common\Model;

use Think\Model;

class NewsModel extends Model
{

    protected $trueTableName = 'news_content';

    public $_validate = array();

    public function addNews()
    {
        $this->_validate = array(
            array('title', 'require', '标题不能为空'),
            array('content', 'require', '内容不能为空'),
        );
        if (!$this->create()) {
            return false;
        }
        $data = I("post.");
        $this->add($data);
        return true;
    }

    /**
     * 获取最新发布信息
     */
    public function getNew(){
        return $this->order('create_time desc')->limit(5)->select();
    }

    public function getSliderData(){
        $type = 2;
        return  $this->where("type = {$type}  and thum != '' ")->order('create_time desc , type desc')->limit(5)->select();
    }

    public function getNewsById($cidArr,$limit){
        $map['cid'] = array('in',$cidArr);
        return $this->where($map)->order('create_time desc')->limit($limit)->select();
    }


    public function getList($cid = null){
        $cid = $cid == null ? I('get.cid',0,'int') : $cid;
        if(!is_array($cid)){
            $cid = explode(',',$cid);
            $cid = array_map('intval',$cid);
        }

        $p = I('p',0,'int');
        $limit = C('DEFALUT_PAGE_SIZE');
        $startNum = $p * $limit;

        if(count($cid) !== 1 && $cid['0'] != 0){
            $map['cid'] = array('in',$cid);
            $count = $this->where($map)->field("count(*) as count")->find();
            $data = $this->where($map)->limit($startNum,$limit)->order('create_time desc')->select();
        }else{
            $count = $this->field("count(*) as count")->find();
            $data = $this->limit($startNum,$limit)->order('create_time desc')->select();
        }
        return array(
            'count' => $count['count'],
            'data'  => $data
        );
    }

    public function edit(){
        $id = I('get.id');
        $data = I('post.');
        $data['id'] = $id;
        $this->save($data);
        return true;
    }

    public function pass(){
        $id = I('get.id');
        $idArr = array_filter(explode(',',$id));
        $idArr =  array_map('intval', $idArr);
        $data['status'] = 1;
        $map['id'] = array('in',$idArr);
        $res = $this->where($map)->save($data);
        return $res;
    }

    public function del(){
        $id = I('get.id');
        $idArr = array_filter(explode(',',$id));
        $idArr =  array_map('intval', $idArr);
        $map['id'] = array('in',$idArr);
        $res = $this->where($map)->delete();
        return $res;
    }

    public function createNews(){
        if(!$this->create()){
            return false;
        }
        return $this->add(I('post.'));
    }

    public function getInfoInId($cidArr)
    {
        if (!empty($cidArr)) {
            $map['cid'] = array('in', $cidArr);
            $data = $this->where($map)->limit(10)->order('click desc')->select();
        } else {
            $data = array();
        }
        return $data;
    }

    public function getHotNews(){
        $field = array('id,title,cid');
        return $this->field($field)->order('type desc')->limit(5)->select();
    }
}
